package utwente.hmi.touchmol.elements;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

/**
 * The turn panel has a single button on it, in the color of the user it is owned by.
 * The user can press this button to take, release or queue for a turn, depending on the protocol.
 * @author Jeroen Logtenberg
 *
 */
public class TurnPanel extends ButtonPanel {

	private static final long serialVersionUID = 1L;
	
	private Dimension currentSize;
	private UserButton userButton;
	private boolean inQueue;
	
	/**
	 * Makes a new turnpanel, a panel with a turn request/release button
	 * for one user.
	 * @param width Width in pixels.
	 * @param height Height in pixels.
	 * @param user The control user. Use TouchMolGUI.USER_[COLOR].
	 */
	public TurnPanel(int width, int height, UserButton user){
		
		inQueue = false;
		
		//int w = UserButton.defaultWidth;
		//int h = UserButton.defaultHeight;
		
		userButton = user;
		
		currentSize = new Dimension(width, height);
		setSize(currentSize);
		setPreferredSize(currentSize);
		setBackground(Color.BLACK);

		setLayout(null);
		add(userButton);
		Rectangle bounds = userButton.getBounds();
		int newX = (width / 2) - (bounds.width / 2);
		int newY = (height / 2) - (bounds.height / 2);
		bounds.x = newX;
		bounds.y = newY;
		userButton.setBounds(bounds);
		
		setVisible(true);
	}

	public UserButton getUserButton(){
		return userButton;
	}
	
	public int getUser(){
		return userButton.getUserID(); 
	}
	
	public boolean isQueued(){
		return inQueue;
	}

	@Override
	public UserButton buttonTouched(Point location) {
		if(userButton.buttonTouched(location)){
			return userButton;
		} else{
			return null;
		}
	}

	@Override
	public void releaseTurn(UserButton user) {
		if(user.getUserID() == getUser()){
			userButton.setBorder(null);
			inQueue = false;	
		}
	}

	@Override
	public void requestTurn(UserButton user) {
		if(user.getUserID() == getUser()){
			userButton.setBorder(UserButton.BORDER_QUEUE);
			inQueue = true;
		}	
	}

	@Override
	public void takeTurn(UserButton user) {
		if(user.getUserID() == getUser()){
			userButton.setBorder(UserButton.BORDER_TURN);
			inQueue = false;
		}
		
	}
}
